package com.crm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.crm.pojo.PageInfo;
import com.crm.pojo.SaleVisit;
import com.crm.service.SaleVisitService;
import com.crm.service.impl.SaleVisitServiceImpl;

public class CustomerVisitServlet extends HttpServlet {
	SaleVisitService visitService = new SaleVisitServiceImpl(); 
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String flag = req.getParameter("flag");
		//进入新增
		if(flag.equals("goadd")) {
			goadd(req,resp);
		}
		//异步获取联系人
		if(flag.equals("queryLkm")) {
			queryLkm(req,resp);
		}
		//新增
		if(flag.equals("add")) {
			add(req,resp);
		}
		//列表查询
		if(flag.equals("query")) {
			query(req,resp);
		}
		//删除
		if(flag.equals("del")) {
			delete(req,resp);
		}
		//进入修改
		if(flag.equals("goedit")) {
			goedit(req,resp);
		}
		//修改
		if(flag.equals("edit")) {
			edit(req,resp);
		}
	}
	

	private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer visitId = Integer.parseInt(req.getParameter("visitId"));
		Integer custId = Integer.parseInt(req.getParameter("custId"));
		Integer lkmId = Integer.parseInt(req.getParameter("lkmId"));
		String time = req.getParameter("time");
		String nexttime = req.getParameter("nexttime");
		String interviewee = req.getParameter("interviewee");
		String addr = req.getParameter("addr");
		String detail = req.getParameter("detail");
		SaleVisit visit = new SaleVisit();
		visit.setVisitId(visitId);
		visit.setVisitCustId(custId);
		visit.setVisitUserId(lkmId);
		visit.setDate(time);
		visit.setVisitInterviewee(interviewee);
		visit.setVisitAddr(addr);
		visit.setVisitDetail(detail);
		visit.setVisitNexttime(nexttime);
		boolean flag = visitService.updateVisit(visit);
		if(flag) {
			resp.sendRedirect(req.getContextPath()+"/success.jsp");
		}else {
			resp.sendRedirect(req.getContextPath()+"/error.jsp");
		}
	}

	private void goedit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取所有客户名称
		List<Map> customers = visitService.queryCustomer();
		//获取拜访记录信息
		Integer visitId = Integer.parseInt(req.getParameter("visitId"));
		Map visit = visitService.queryVisitById(visitId);
		//获取当前记录客户的所有联系人
		Integer custId = Integer.parseInt(visit.get("visit_cust_id").toString());
		List<Map> lkms = visitService.queryLkm(custId);
		req.setAttribute("lkms", lkms);
		req.setAttribute("customers", customers);
		req.setAttribute("visit", visit);
		req.getRequestDispatcher("customer_visit/edit.jsp").forward(req, resp);
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer visitId = Integer.parseInt(req.getParameter("visitId"));
		boolean flag = visitService.delete(visitId);
		if(flag) {
			resp.sendRedirect(req.getContextPath()+"/success.jsp");
		}else {
			resp.sendRedirect(req.getContextPath()+"/error.jsp");
		}
	}

	private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String custName = req.getParameter("custName");
		if(custName==null) {
			custName = "";
		}
		Integer pageSize;
		if(req.getParameter("pageSize")!=null) {
			pageSize = Integer.parseInt(req.getParameter("pageSize"));
		}else {
			pageSize = 15;
		}
		Integer pageNumber;
		if(req.getParameter("pageNumber")!=null) {
			pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
		}else {
			pageNumber = 1;
		}
		PageInfo pageVisit = visitService.queryVisit(custName,pageSize,pageNumber);
		req.setAttribute("custName", custName);
		req.setAttribute("pageVisit", pageVisit);
		req.getRequestDispatcher("customer_visit/list.jsp").forward(req, resp);
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer custId = Integer.parseInt(req.getParameter("custId"));
		Integer lkmId = Integer.parseInt(req.getParameter("lkmId"));
		String time = req.getParameter("time");
		String nexttime = req.getParameter("nexttime");
		String interviewee = req.getParameter("interviewee");
		String addr = req.getParameter("addr");
		String detail = req.getParameter("detail");
		SaleVisit visit = new SaleVisit();
		visit.setVisitCustId(custId);
		visit.setVisitUserId(lkmId);
		visit.setDate(time);
		visit.setVisitInterviewee(interviewee);
		visit.setVisitAddr(addr);
		visit.setVisitDetail(detail);
		visit.setVisitNexttime(nexttime);
		boolean flag = visitService.insertVisit(visit);
		if(flag) {
			resp.sendRedirect(req.getContextPath()+"/success.jsp");
		}else {
			resp.sendRedirect(req.getContextPath()+"/error.jsp");
		}
	}
	
	private void queryLkm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer custId = Integer.parseInt(req.getParameter("custId"));
		List<Map> lkms = visitService.queryLkm(custId);
		String lkm = JSON.toJSONString(lkms);
		PrintWriter writer = resp.getWriter();
		writer.print(lkm);
	}

	private void goadd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取所有客户信息
		List<Map> customers = visitService.queryCustomer();
		//获取第一个客户的联系人
		Integer custId = Integer.parseInt(customers.get(0).get("cust_id").toString());
		List<Map> lkms = visitService.queryLkm(custId);
		req.setAttribute("lkms", lkms);
		req.setAttribute("customers", customers);
		req.getRequestDispatcher("customer_visit/add.jsp").forward(req, resp);
	}


}
